#include "attack.h"
#include "Ai.hpp"
#include "helper.h"

#define AGGRO_RADIUS 3

bool Attack::execute() {
    bool target_acquired = false;
    for( map<int,Fruit>::iterator it=ai.world.theirFruits.begin(); it != ai.world.theirFruits.end(); it++ ){
        if( distance(it->second.position, idParent->position) < AGGRO_RADIUS*AGGRO_RADIUS ){
            target_acquired = true;
            target = &it->second;
            if( idParent->attack( target->id ) == OK ) return true;
            pf.start = idParent->position;
            pf.end = target->position;
            path.clear();
            if( pf.compute( &path, 0, 0 ) ){
                if( path.size() < idParent->speed ){
                    list<map_chunk*>::reverse_iterator it2 = path.rbegin();
                    it2++; it2++;
                    if( idParent->move( (*it2)->X_coord, (*it2)->Y_coord ) == OK ) return true;
                }
            }
        }
    }
    pf.end = &ai.world.at( x, y );
    path.clear();
    if( pf.compute( &path, 0, 0 ) ){
        list<map_chunk*>::iterator it = path.begin();
        for( int i = 0; i < idParent->speed; i++ ) if( it != path.end() )it++;
        if( it == path.end() ) it--;
        if( idParent->move( (*it)->X_coord, (*it)->Y_coord ) == OK ) return true;
    }
    return false;
}


